<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * website short summary.
 *
 * website description.
 *
 * @version 1.0
 * @author xianbin
 */
class website extends Sys_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->Model('Website_model');
        $this->load->Model('Menu_model');
        $this->load->library('ajaxMsgModel');
        $this->load->library('form_validation');
    }
    public function index()
    {
        $this->ajaxmsgmodel->Type='1';
        $this->Ajax_Check($this->ajaxmsgmodel);

        $data['up_first_website_model']=$this->Website_model->get_website_by_type_weight(0,1);
        $data['up_second_website_model']=$this->Website_model->get_website_by_type_weight(0,2);
        $data['up_third_website_model']=$this->Website_model->get_website_by_type_weight(0,3);

        $data['down_first_website_model']=$this->Website_model->get_website_by_type_weight(1,1);
        $data['down_second_website_model']=$this->Website_model->get_website_by_type_weight(1,2);
        $data['down_third_website_model']=$this->Website_model->get_website_by_type_weight(1,3);
        $data['down_four_website_model']=$this->Website_model->get_website_by_type_weight(1,4);

        $top_menu_list=$this->Menu_model->get_all_top_menu();

        //栏目
        foreach($top_menu_list as $top_menu_list_item)
        {
            if($top_menu_list_item->child==1 && $top_menu_list_item->isdrop==1)
            {
                $top_menu_list_item->child_list=$this->Menu_model->get_all_child_menu_by_id($top_menu_list_item->menuid);
                foreach($top_menu_list_item->child_list as $top_menu_list_item_childs_item)
                {
                    $first_child_menu_model=null;
                    $this->get_max_dept_first_menu($top_menu_list_item_childs_item,$first_child_menu_model);
                    if(!empty( $first_child_menu_model))
                    {
                        $top_menu_list_item_childs_item->page_url=site_url($first_child_menu_model->menu_control."/".$first_child_menu_model->menu_action.($first_child_menu_model->page_type==1?'list':'info')."/".$first_child_menu_model->menuid);
                    }
                }
            }
            $first_child_menu_model=null;
            $this->get_max_dept_first_menu($top_menu_list_item,$first_child_menu_model);
            if(!empty( $first_child_menu_model))
            {
                $top_menu_list_item->page_url=site_url($first_child_menu_model->menu_control."/".$first_child_menu_model->menu_action.($first_child_menu_model->page_type==1?'list':'info')."/".$first_child_menu_model->menuid);
            }

        }
        $data['top_menu_list']=$top_menu_list;

        $this->load->Model('Banner_model');
        $banner_list=$this->Banner_model->get_all_banner();
        $data['banner_list']=$banner_list;

        //网站信息
        $this->load->Model('Config_model');
        $data['config_model']= $this->Config_model-> Get_Config_FirstRow();

        //联系信息
        $this->load->Model('Contact_model');
        $data['contact_model']=$this->Contact_model->get_first_row();
        $this->load->view('Admin/website/index.html',$data);
    }

    public function edit_home()
    {
        $this->ajaxmsgmodel->Type='3';
        $this->Ajax_Check($this->ajaxmsgmodel);
        $all_menu=$this->Menu_model->get_all_menu();
        $website_id=$this->input->get('websiteid');
        $website_type=$this->input->get('website_type');
        $weight=$this->input->get('weight');
        $menuid=$this->input->get('menuid');
        $data['website_model']=$this->Website_model->get_website_by_id_weight($website_id,$weight);
        $data['website_type']=$website_type;
        $data['website_id']=$website_id;
        $data['weight']=$weight;
        $data['menuid']=$menuid;
        $menu_select_url=array();
        foreach($all_menu as $menu_item)
        {
            $first_child_menu_model=null;
            $this->get_max_dept_first_menu($menu_item,$first_child_menu_model);

            $menu_drop_url=site_url($first_child_menu_model->menu_control."/".$first_child_menu_model->menu_action.($first_child_menu_model->page_type==1?'list':'info')."/".$first_child_menu_model->menuid);
            $menu_select_url[]=array($menu_drop_url,$menu_item->title,$menu_item->menuid);
        }
        //p($menu_select_url);
        $data['menu_select_url']=$menu_select_url;
        $this->load->view('Admin/website/edit_home.html',$data);
    }

    public function ajax_eidt_website()
    {
        $this->ajaxmsgmodel->Type='4';
        $this->Ajax_Check($this->ajaxmsgmodel);

        $website_model=new $this->Website_model();
        $website_model->website_id=$this->input->post('website_id');
        $website_model->eng_title=$this->input->post('eng_title');
        $website_model->imgurl=$this->input->post('imgurl');
        $website_model->website_url=$this->input->post('website_url');
        $website_model->weight=$this->input->post('weight');
        $website_model->website_type=$this->input->post('website_type');
        $website_model->title=$this->input->post('title');
        $website_model->menuid=$this->input->post('menuid');
        if($website_model->website_id>0)//编辑
        {
            $res= $this->Website_model->update_website($website_model);
            if($res>0)
            {
                $this->ajaxmsgmodel->Msg='修改成功';
                $this->ajaxmsgmodel->Statu='ok';
                $this->ajaxmsgmodel->BackUrl=site_url('admin/website/index');
            }
            else
            {
                $this->ajaxmsgmodel->Msg='修改失败';
                $this->ajaxmsgmodel->Statu='err';
            }
        }else//添加
        {
            $res= $this->Website_model->add_website($website_model);
            if($res>0)
            {
                $this->ajaxmsgmodel->Msg='添加成功';
                $this->ajaxmsgmodel->Statu='ok';
                $this->ajaxmsgmodel->BackUrl=site_url('admin/website/index');
            }
            else
            {
                $this->ajaxmsgmodel->Msg='修改失败';
                $this->ajaxmsgmodel->Statu='err';
            }
        }
        echo(json_encode($this->ajaxmsgmodel));
    }

    public function del_website()
    {
        $this->ajaxmsgmodel->Type='4';
        $this->Ajax_Check($this->ajaxmsgmodel);

        $website_id=$this->uri->segment(4);
        $back_url=site_url('admin/website/index');
        $res=$this->Website_model->del_website($website_id);
        if($res>0)
        {
            $this->ajaxmsgmodel->Msg='删除成功';
            $this->ajaxmsgmodel->Statu='ok';
            $this->ajaxmsgmodel->BackUrl=$back_url;
        }
        else
        {
            $this->ajaxmsgmodel->Msg='删除失败';
            $this->ajaxmsgmodel->Statu='err';
        }
        echo(json_encode($this->ajaxmsgmodel));
    }
}